package jude.java.money.tools;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jude.java.money.batch.Config;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang.StringUtils;

public class Ping {

	private static final String ENCODING = "UTF-8";
	
	private static final String[] URLS = {
		"http://www.websiteoutlook.com/whois/${SHORT_DOMAIN}",
		"http://www.statbrain.com/${DOMAIN}",
		"http://builtwith.com/${SHORT_DOMAIN}",
		"http://snapshot.compete.com/${SHORT_DOMAIN}/",
		"http://www.aboutus.org/${SHORT_DOMAIN}",
		"http://www.quantcast.com/${SHORT_DOMAIN}",
		"http://www.cubestat.com/${DOMAIN}",
		"http://whois.tools4noobs.com/info/${SHORT_DOMAIN}",				//http://whois-verify.com/info/${SHORT_DOMAIN}
		"http://www.alexa.com/siteinfo/${SHORT_DOMAIN}",
//		"http://www.alexa.com/data/details/?url=${SHORT_DOMAIN}",
		"http://www.siteadvisor.cn/sites/${SHORT_DOMAIN}/summary/",
//		"http://whois.domaintools.com/${SHORT_DOMAIN}",						//需要注册
		"http://www.aboutdomain.org/backlinks/${SHORT_DOMAIN}/",
		"http://www.whoisya.com/${SHORT_DOMAIN}",
		"http://www.who.is/whois/${SHORT_DOMAIN}",
		"http://www.robtex.com/dns/${SHORT_DOMAIN}.html",
//		"http://www.robtex.com/whois/${SHORT_DOMAIN}",						//
//		"http://www.zimbio.com/search?q=${SHORT_DOMAIN}&type=all",			//Forbidden(&btnG=Search), 站内搜索
		"http://www.who.is/whois/${SHORT_DOMAIN}/",
		"http://whoisx.co.uk/${SHORT_DOMAIN}",
		"http://searchanalytics.compete.com/site_referrals/${SHORT_DOMAIN}",
		"http://www.xomreviews.com/${SHORT_DOMAIN}",
		"http://seedspiller.com/${SHORT_DOMAIN}",							//http://seedspills.com
//		"http://www.protectwebform.com/stats/site.php?d=${SHORT_DOMAIN}",	//内部错误
		"http://www.similarsites.com/site/${SHORT_DOMAIN}",
		"http://dntrace.keyhints.com/${SHORT_DOMAIN}",
		"http://www.markosweb.com/www/${SHORT_DOMAIN}/",
		"http://www.statsmogul.com/www.${DOMAIN}",							//"http://www.peekstats.com/${SHORT_DOMAIN}",
		"http://websitevaluebot.com/${DOMAIN}",
//		"http://www.peekstats.com/www.${SHORT_DOMAIN}",
//		"http://worthbot.com/${DOMAIN}",									//UnknownHostException
		"http://websitevaluecalculator.org/${DOMAIN}",
		"http://website-value.net/${DOMAIN}",
		"http://webworth.info/${SHORT_DOMAIN}",
		"http://statswebsites.com/${DOMAIN}",
//		"http://cash81.com/${DOMAIN}",										//未知跳转
		"http://nakedweb.org/${DOMAIN}",
//		"http://tatlia.com/${DOMAIN}",										//打不开
		"http://statout.com/${DOMAIN}",
		"http://georanks.com/${DOMAIN}",
		"http://webrapport.net/${DOMAIN}",
//		"http://worthlook.com/${DOMAIN}",									//UnknownHostException
		"http://worth.im/${DOMAIN}",
		"http://www.sitedossier.com/anchor/${DOMAIN}",
		"http://www.sitedossier.com/site/${SHORT_DOMAIN}",
		"http://www.sitedossier.com/search?q=${DOMAIN}",
		"http://www.aboutthedomain.com/${SHORT_DOMAIN}",
		"http://wholinkstome.com/url/${SHORT_DOMAIN}",
		"http://www.websiteaccountant.nl/${DOMAIN}",
		"http://www.webmaster-rank.info/?pourcent-${DOMAIN}",
		"http://websiteshadow.com/${SHORT_DOMAIN}",
		"http://www.talkreviews.com/${SHORT_DOMAIN}",
		"http://www.ip-adress.com/whois/${DOMAIN}",
		"http://www.listenarabic.com/${DOMAIN}",
//		"http://boardreader.com/domain/${SHORT_DOMAIN}",					//打不开
		"http://www.keywordspy.com/organic/domain.aspx?q=${SHORT_DOMAIN}",
		"http://dataopedia.com/${SHORT_DOMAIN}",							//${DASH_SHORT_DOMAIN}
		"http://freeprchecker.com/${DOMAIN}",								//
		"http://www.serpanalytics.com/sites/${DOMAIN}",
		"http://www.quarkbase.com/${SHORT_DOMAIN}",
		"http://stimator.com/confirmation/${SHORT_DOMAIN}",					//"http://stimator.com/${DASH_SHORT_DOMAIN}",
		"http://www.valuatemysite.com/${DOMAIN}",
		"http://websitevaluecalculate.com/${DOMAIN}",
		"http://tubeurl.com/${SHORT_DOMAIN}",								//打不开
		"http://www.snuse.no/search_web2.php?mode=2&swg=2&swy=2&list=1&pt=1&er=1&ex=&ttex=site:${DOMAIN}",
		"http://www.sitelogr.com/s/${SHORT_DOMAIN}",						//空白
		"http://www.websiteaccountant.be/${DOMAIN}",
		"http://serversiders.com/${SHORT_DOMAIN}",
		"http://bizinformation.org/us/${DOMAIN}",
		"http://www.pagerankplace.com/website/${SHORT_DOMAIN}",
		
		"http://push2check.com/${SHORT_DOMAIN}",
		
		"http://site.biniu.com/?q=${SHORT_DOMAIN}",
		"http://site.biniu.com/?q=www.${SHORT_DOMAIN}",
	};
	
	public void ping() {
		for (int j = 0; j < URLS.length; j++) {
			String[][] sites = Config.Zencart.SITES;
			try {
				for (int i = 0; i < sites.length; i++) {
					for (int k = 0; k < sites[i].length; k++) {
						String domain = sites[i][k];
						String url = getUrl(URLS[j], domain);
						System.err.println(url);
						submit(url);
					}
				}
			} catch (Exception e) {
				
			}
		}
	}
	
	void out() {
		for (int j = 0; j < URLS.length; j++) {
			String url = URLS[j];
			url = StringUtils.replace(url, "${SHORT_DOMAIN}", "{website}");
			url = StringUtils.replace(url, "${DOMAIN}", "{website}");
			System.out.println(url);
		}
	}
	
	private String getUrl(String url, String domain) {
		String shortDoamin = domain;
		if (domain.startsWith("www."))
			shortDoamin = domain.substring(4);
		if (url.contains("${DOMAIN}")) {
			url = StringUtils.replace(url, "${DOMAIN}", domain);
		} else if (url.contains("${SHORT_DOMAIN}")) {
			url = StringUtils.replace(url, "${SHORT_DOMAIN}", shortDoamin);
		} else if (url.contains("${DASH_SHORT_DOMAIN}")) {
			String dashDoamin = shortDoamin.replace('.', '-');
			url = StringUtils.replace(url, "${DASH_SHORT_DOMAIN}", dashDoamin);
		} else {
			System.err.println("invalid url: " + url);
		}
		return url;
	}
	
	private HttpClient getHttpClient() {
		HttpClient client = new HttpClient();
		HttpConnectionManagerParams p = client.getHttpConnectionManager().getParams();
		p.setConnectionTimeout(30000);
		p.setSoTimeout(60000);
		return client;
	}
	
	private Ping out(Object str) {
		System.out.print(str);
		return this;
	}
	private Ping outln(Object str) {
		System.out.println(str);
		return this;
	}
	
	private GetMethod getGetMethod(String url, String params) {
		if (StringUtils.isNotEmpty(params)) {
			if (url.indexOf('?') > 0) {
				if (params.charAt(0) == '?')
					url += params.substring(1);
				else
					url += params;
			} else {
				if (params.charAt(0) != '?')
					url += '?' + params;
				else
					url += params;
			}
		}
		GetMethod get = new GetMethod(url);
		get.getParams().setParameter(HttpClientParams.HTTP_CONTENT_CHARSET, ENCODING);
		get.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandler() {
			@Override
			public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {
				return !(exception instanceof ConnectException) && executionCount < 3;
			}
		});
		return get;
	}
	
	private void submit(String url) throws HttpException, IOException {
		HttpClient client = getHttpClient();
		GetMethod get = getGetMethod(url, null);
//		get.setFollowRedirects(false);
		int statusCode = client.executeMethod(get);
		if (statusCode != HttpStatus.SC_OK) {
			out("HTTP-").outln(statusCode);
			return;
		}
		String path = get.getURI().toString();
		if (!url.equals(path)) {
			System.out.println("- - " + path);
		}
		InputStream in = get.getResponseBodyAsStream();
		StringBuilder sb = read(in, ENCODING, 8192);
		String regex = "<title>(.+?)</title>";
		Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
		Matcher m = p.matcher(sb);
		if (m.find()) {
			outln(m.group(1));
		} else {
			outln("unknown");
		}
	}
	
	public static StringBuilder read(InputStream in, String encode, int max) {
		try {
			StringBuilder r = new StringBuilder();
			Reader reader = new InputStreamReader(in, encode);
			char[] buffer = new char[4096];
			int n, t = 0;
			while ((n = reader.read(buffer)) > 0) {
				r.append(buffer, 0, n);
				if (max > 0) {
					t += n;
					if (t >= max)
						break;
				}
			}
			in.close();
			return r;
		} catch (Exception e) {
		}
		return null;
	}
	
	static String encode(String str) throws UnsupportedEncodingException {
		return URLEncoder.encode(str, ENCODING);
	}
	
	public static void main(String[] args) {
		new Ping().out();
	}
}
